//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_gitee"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processGiteeEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		pushEvent := &events_gitee.PushEvent{}
		if event.EventData.MessageIs(pushEvent) {
			err = event.EventData.UnmarshalTo(pushEvent)
			if err != nil {
				return false, err
			}
			repoUrl := ""
			projectName := ""
			if len(pushEvent.Project) > 0 {
				repoUrl = pushEvent.Project[0].Url
				projectName = pushEvent.Project[0].FullName
			}
			srcUser := ""
			if len(pushEvent.Sender) > 0 {
				srcUser = pushEvent.Sender[0].Name
			}
			lastCommit := ""
			if len(pushEvent.Commits) > 0 {
				lastCommit = pushEvent.Commits[len(pushEvent.Commits)-1].Msg
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s 推送 %s(%s),包含%d次提交,最后提交内容 %s。", event.UserDisplayName, event.ProjectName,
				srcUser, projectName, repoUrl, pushEvent.TotalCommitsCount, lastCommit)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GiteeEvCfg.Push {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		issueEvent := &events_gitee.IssueEvent{}
		if event.EventData.MessageIs(issueEvent) {
			err = event.EventData.UnmarshalTo(issueEvent)
			if err != nil {
				return false, err
			}
			action := ""
			if issueEvent.Action == "open" {
				action = "新建工单"
			} else if issueEvent.Action == "state_change" {
				action = "修改工单状态"
			} else if issueEvent.Action == "delete" {
				action = "删除工单"
			}
			issueTitle := ""
			issueUrl := ""
			if len(issueEvent.Issue) > 0 {
				issueTitle = issueEvent.Issue[0].Title
				issueUrl = issueEvent.Issue[0].HtmlUrl
			}
			srcUser := ""
			if len(issueEvent.Sender) > 0 {
				srcUser = issueEvent.Sender[0].Name
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s %s %s(%s)。", event.UserDisplayName, event.ProjectName,
				srcUser, action, issueTitle, issueUrl)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GiteeEvCfg.Issue {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		pullRequestEvent := &events_gitee.PullRequestEvent{}
		if event.EventData.MessageIs(pullRequestEvent) {
			err = event.EventData.UnmarshalTo(pullRequestEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s TODO", event.UserDisplayName, event.ProjectName)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GiteeEvCfg.PullRequest {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}

	}

	{
		noteEvent := &events_gitee.NoteEvent{}
		if event.EventData.MessageIs(noteEvent) {
			err = event.EventData.UnmarshalTo(noteEvent)
			if err != nil {
				return false, err
			}
			commentUrl := ""
			commentTitle := ""
			if noteEvent.Comment != nil {
				commentUrl = noteEvent.Comment.HtmlUrl
				commentTitle = noteEvent.Comment.Body
			}
			issueUrl := ""
			if len(noteEvent.Issue) > 0 {
				issueUrl = noteEvent.Issue[0].HtmlUrl
			}
			srcUser := ""
			if len(noteEvent.Sender) > 0 {
				srcUser = noteEvent.Sender[0].Name
			}
			msg = fmt.Sprintf("%s 在项目 %s 源用户 %s 对工单 %s(%s) 发布评论 %s(%s)。", event.UserDisplayName, event.ProjectName,
				srcUser, noteEvent.Title, issueUrl, commentTitle, commentUrl)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.GiteeEvCfg.Note {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
